package com.zhiyou100.crm.servlet.notice;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.Notice;
import com.zhiyou100.crm.service.NoticeService;
import com.zhiyou100.crm.service.impl.NoticeServiceImpl;

/**
 * 更新通知公告
 */
@WebServlet("/notice/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	NoticeService noticeService=new NoticeServiceImpl();
	Notice notice=null;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String id = request.getParameter("id");
		int noticeId = Integer.parseInt(id);
		try {
			notice=noticeService.queryNoticeById(noticeId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(notice!=null) {
			request.setAttribute("notice", notice);
			request.getSession().setAttribute("id", id);
			request.getRequestDispatcher("/WEB-INF/view/notice/update.jsp").forward(request, response);
		}else {
			response.sendRedirect(request.getContextPath()+"/list.jsp");
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取更新内容
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-mm-dd");
		Object id = request.getSession().getAttribute("id");
		int id1=Integer.parseInt((String)id);
		String pubTime = request.getParameter("pubTime");
		String expireTime = request.getParameter("expireTime");
		String subject = request.getParameter("subject");
		int receiveId = Integer.parseInt((String)request.getParameter("receiveId"));
		String text=request.getParameter("text");
		int noticeId = Integer.parseInt((String)request.getParameter("noticeId"));
		try {
			Timestamp pubTime1=new Timestamp(format2.parse(pubTime).getTime());
			Timestamp expireTime1=new Timestamp(format2.parse(expireTime).getTime());
			notice=new Notice(noticeId,receiveId,subject,text,pubTime1,expireTime1,id1);
			int a=noticeService.upadteNotice(notice);
			if(a>0){
				response.sendRedirect(request.getContextPath()+"/notice/list");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}catch (Exception e){
			e.printStackTrace();

		}


	}

}
